package com.yonyou.crm.bpub.detail.service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.bpub.detail.entity.DetailsFlagVO;
import com.yonyou.crm.bpub.detail.entity.DetailsGroupVO;
import com.yonyou.crm.bpub.detail.entity.DetailsItemVO;
import com.yonyou.crm.sprc.opportunity.entity.OpportunityBVO;
import com.yonyou.crm.sprc.opportunity.entity.OpportunityStateEnum;
import com.yonyou.crm.sprc.opportunity.entity.OpportunityVO;
import com.yonyou.crm.sprc.opportunity.service.OpportunityBizServiceImpl;
import com.yonyou.crm.sys.user.entity.UserVO;
import com.yonyou.iuap.context.InvocationInfoProxy;

@Service
public class OppDetail extends BaseDetails {

	@Autowired
	OpportunityBizServiceImpl service;
	
	@Override
	public <T extends Serializable> T getDetail(long id) {
		OpportunityVO obj = service.getDetail(id);
		if ((obj != null) && (obj.getId() == null)) {
			obj = null;
		}
		return (T)obj;
	}
	
	/**
	 * 获取详情模板
	 */
	@Override
	public  <T extends Serializable> Map<String, List<DetailsGroupVO>> getLayout(T obj) {
		OpportunityVO vo = (OpportunityVO) obj;
		//TODO定义biztype
		//Long biztypeId = vo.getBiztypeId();
		long biztypeId = 11;
		return layoutService.getMobileDetailTemplateForCurrentUser(biztypeId);
	}
	
	@Override
	public <T extends Serializable> List<DetailsGroupVO> getHeader(T obj, Map<String, List<DetailsGroupVO>> layoutList) {
		List<DetailsGroupVO> headerlist = new ArrayList<>();
		
		OpportunityVO vo = (OpportunityVO)obj;

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		DetailsGroupVO header = new DetailsGroupVO();
		List<DetailsItemVO> headerData = new ArrayList<>();
		DetailsItemVO item;
		
		item = new DetailsItemVO();
		item.setItemkey("name");
		item.setName("商机名称");
		item.setValue(vo.getName());
		item.setType("singlelinetext");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("customerId");
		item.setName("客户名称");
		item.setId((vo.getCustomerId() == null) ? "" : vo.getCustomerId().toString());
		item.setValue((vo.getCustomerInfo() == null || vo.getCustomerInfo().getName() == null) ? "" : vo.getCustomerInfo().getName());
		item.setType("refertype");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("saleStage");
		item.setName("商机阶段");
		item.setId((vo.getSaleStage() == null) ? "" : vo.getSaleStage().toString());
		item.setValue((vo.getSaleStageName() == null) ? "" : vo.getSaleStageName());
		item.setType("refertype");
		headerData.add(item);
		
		//TODO赢单、输单里不显示停留时间
		item = new DetailsItemVO();
		item.setItemkey("stageStartTime");
		item.setName("停留时间");
		String stageTime = "";
		if (vo.getStageStartTime() == null) {
			stageTime = "0天0小时";
		} else {
			Date stageStartTime = vo.getStageStartTime();
			Date nowTime = new Date();
			long between = nowTime.getTime() - stageStartTime.getTime();
			long day = between/(24*3600*1000);
			long hour = between%(24*3600*1000)/(3600*1000);
			stageTime = day + "天" + hour + "时";
		}
		
		item.setValue("停留"+stageTime);
		item.setType("singlelinetext");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("expectSignMoney");
		//TODO 根据商机状态返回不同的金额，跟进、输单显示预计签单金额，赢单显示实际签单金额，显示文本也不同
		if (vo.getState() == 1) {
			item.setName("实际签单金额");
			item.setValue((vo.getActualSignMoney() == null) ? "" : transformPrice(vo.getActualSignMoney()));
		} else {
			item.setName("预计签单金额");
			item.setValue((vo.getExpectSignMoney() == null) ? "" : transformPrice(vo.getExpectSignMoney()));
		}
		item.setType("money");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("stageFlag");
		item.setValue((vo.getStageFlag() > 0) ? "Y" : "N");
		item.setType("flag");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("state");
		item.setValue(String.valueOf(vo.getState()));
		item.setType("enum");
		headerData.add(item);
		
		header.setGroupdata(headerData);
		headerlist.add(header);
		
		header = new DetailsGroupVO();
		headerData = new ArrayList<>();
		
		item = new DetailsItemVO();
		item.setItemkey("winProbability");
		item.setName("赢单概率");
		item.setValue(vo.getWinProbability());
		item.setType("singlelinetext");
		headerData.add(item);

		item = new DetailsItemVO();
		item.setItemkey("expectSignTime");
		item.setName("预计签单时间");
		item.setValue((vo.getExpectSignTime() == null) ? "" : sdf.format(vo.getExpectSignTime()));
		item.setType("date");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("expectSignMoney");
		item.setName("预计签单金额");
		item.setValue((vo.getExpectSignMoney() == null) ? "" : vo.getExpectSignMoney().toString());
		item.setType("money");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("ownerUserId");
		item.setName("负责人");
		item.setId((vo.getOwnerUserId() == null) ? "" : vo.getOwnerUserId().toString());
		item.setValue((vo.getOwnerUserName() == null) ? "" : vo.getOwnerUserName());
		item.setType("refertype");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("relUsers");
		List<UserVO> relUserList = vo.getRelUserList();
		String dataid = "";
		String datavalue = "";
		if (relUserList != null){
			for (UserVO user:relUserList) {
				if (dataid.isEmpty()) {
					dataid = user.getId().toString();
					datavalue = user.getName();
				} else {
					dataid = dataid + "," + user.getId().toString();
					datavalue = datavalue + "," + user.getName();
				}
			}
		}
		item.setId(dataid);
		item.setName("参与人");
		item.setValue(datavalue);
		item.setType("refertype");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("type");
		item.setName("商机类型");
		item.setId((vo.getType() == null) ? "" : vo.getType().toString());
		item.setValue((vo.getTypeName() == null) ? "" : vo.getTypeName());
		item.setType("enum");
		headerData.add(item);

		item = new DetailsItemVO();
		item.setItemkey("customerBudget");
		item.setName("客户预算");
		item.setValue((vo.getCustomerBudget() == null) ? "" : vo.getCustomerBudget().toString());
		item.setType("money");
		headerData.add(item);

		item = new DetailsItemVO();
		item.setItemkey("createdTime");
		item.setName("商机日期");
		item.setValue((vo.getCreatedTime() == null) ? "" : sdf.format(vo.getCreatedTime()));
		item.setType("date");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("description");
		item.setName("备注");
		item.setValue((vo.getDescription() == null) ? "" : vo.getDescription());
		item.setType("largetext");
		headerData.add(item);
		
		header.setGroupdata(headerData);
		headerlist.add(header);
		
		return headerlist;
	}

	@Override
	public <T extends Serializable> List<DetailsGroupVO> getBody(T obj, Map<String, List<DetailsGroupVO>> layoutList) {
		
		List<DetailsGroupVO> bodyList = new ArrayList<>();
		OpportunityVO vo = (OpportunityVO)obj;

		if (vo == null) {
			return bodyList;
		}
		List<OpportunityBVO> bvos = vo.getChildList();
		if (bvos == null || bvos.size() == 0) {
			return bodyList;
		}
		for (OpportunityBVO bvo : bvos) {
			DetailsGroupVO body = new DetailsGroupVO();
			List<DetailsItemVO>  list = new ArrayList<>();
			
			DetailsItemVO item;
			
			item = new DetailsItemVO();
			item.setItemkey("productId");
			item.setName("产品名称");
			item.setId((bvo.getProductId() == null) ? "" : bvo.getProductId().toString());
			item.setValue((bvo.getProductName() == null) ? "" : bvo.getProductName());
			item.setType("refertype");
			list.add(item);
			
			item = new DetailsItemVO();
			item.setItemkey("productTypeId");
			item.setName("产品分类");
			item.setId((bvo.getProductTypeId() == null) ? "" : bvo.getProductTypeId().toString());
			item.setValue((bvo.getProductTypeName() == null) ? "" : bvo.getProductTypeName());
			item.setType("refertype");
			list.add(item);
			
			item = new DetailsItemVO();
			item.setItemkey("brandId");
			item.setName("品牌");
			item.setId((bvo.getBrandId() == null) ? "" : bvo.getBrandId().toString());
			item.setValue((bvo.getBrandName() == null) ? "" : bvo.getBrandName());
			item.setType("refertype");
			list.add(item);
			
			item = new DetailsItemVO();
			item.setItemkey("price");
			item.setName("销售单价");
			item.setValue((bvo.getPrice() == null) ? "" : bvo.getPrice().toString());
			item.setType("money");
			list.add(item);

			item = new DetailsItemVO();
			item.setItemkey("number");
			item.setName("数量");
			item.setValue((bvo.getNumber() == null) ? "" : bvo.getNumber().toString());
			item.setType("decimal");
			list.add(item);
			
			body.setGroupdata(list);
			bodyList.add(body);
		}
		
		return bodyList;
	}
	
	@Override
	public <T extends Serializable> List<DetailsFlagVO> getFlag(T obj){
		OpportunityVO vo = (OpportunityVO)obj;
		List<DetailsFlagVO> flags = new ArrayList<>();
		DetailsFlagVO flag;
		String flagValue;
		
		flag = new DetailsFlagVO();
		flag.setDes("follow");
		flagValue = (vo.getFollowFlag().equals("1")) ? "Y" : "N";
		flag.setValue(flagValue);
		flags.add(flag);
		
		return flags;
	}
	
	@Override
	public <T extends Serializable> List<String> getAction(T obj){
		List<String> actions = new ArrayList<>();
		OpportunityVO vo = (OpportunityVO) obj;
		Long userId = new Long(InvocationInfoProxy.getParameter("id"));
		if (userId.toString().equals(vo.getOwnerUserId().toString()) && vo.getState() == OpportunityStateEnum.FOLLOW.getValue()) {
			actions.add("edit");
			actions.add("win");
			actions.add("lost");
		}
		
		return actions;
	}
	
	//格式化金额类型数据
	public String transformPrice(BigDecimal price) {
		if (price == null) {
			return "";
		}
		DecimalFormat df2 = new DecimalFormat("#,###.00");
        String priceStr = df2.format(price);
        return priceStr;
	}

}
